#ifndef PRESENTERMONITOR_H
#define PRESENTERMONITOR_H

#include "liverecordertype.h"
#include <QObject>

QT_BEGIN_NAMESPACE
class QTimer;
QT_END_NAMESPACE

namespace yc {
struct Attention;
}

class LiveRecorder;

class PresenterMonitorWorker : public QObject
{
    Q_OBJECT

public:
    explicit PresenterMonitorWorker(LiveRecorder* recorder, QObject* parent = nullptr);

    int presenter() const { return presenter_; }
    bool isDone() const { return done_; }
    void start(int presenter);
    void stop();

private:
    void updatePresenterStatus(int status, int active_live, int audience_count,
                               const QDateTime& update_time, int updater);

    bool getLiveStatus(int platform, const QString& presenter_platform_id,
                       const LiveHandler& handler);
    void onGetStatus(int presenter, const QString& args);

    LiveRecorder* recorder_;
    int presenter_ = 0;
    bool done_ = true;
};

class PresenterRealtimeMonitorWorker : public QObject
{
    Q_OBJECT

public:
    explicit PresenterRealtimeMonitorWorker(LiveRecorder* recorder, QObject* parent = nullptr);

    int presenter() const { return presenter_; }
    bool isDone() const { return done_; }
    void start(int presenter);
    void stop();

private:
    bool subscribe(int presenter, int platform, const QString& presenter_platform_id,
                   const LiveHandler& handler);
    void unsubscribe(int presenter);
    void onGetStatus(int presenter, const QString& args);

    LiveRecorder* recorder_;
    int presenter_ = 0;
    bool done_ = true;
    QTimer* timer_;
};

class PresenterMonitor : public QObject
{
    Q_OBJECT
public:
    explicit PresenterMonitor(LiveRecorder* recorder, QObject* parent = nullptr);

    void start();
    void stop();
    void setRealtime(int presenter, bool realtime);

private:
    void getLiveStatus();
    void removeAttention(yc::Attention* attention);

    LiveRecorder* recorder_;
    bool started_ = false;
    QList<PresenterRealtimeMonitorWorker*> realtime_workers_;
    QList<PresenterMonitorWorker*> workers_;
    QTimer* timer_;
    int pos_ = 0;
};

#endif // PRESENTERMONITOR_H
